package com.aaa.controller.back;

import com.aaa.entity.Emp;
import com.aaa.entity.Modules;
import com.aaa.service.back.EmpService;
import com.aaa.service.back.ModulesService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/back")
@Controller
public class LoginController {
    @Resource
    private EmpService empService;

    @Resource
    private ModulesService modulesService;


    @RequestMapping("/login")
    public String login() {

        return "back/login";
    }


    @RequestMapping("/login_submit")
    public String login_submit(Model model, String empUserName, String empPassword, HttpSession session) {
        Emp a = empService.findEmpByName(empUserName,empPassword);
        if(a==null){
            model.addAttribute("err","用户名或密码为空");
            return "back/login";
        }else{
            if(a.getEmpState()==1){
                model.addAttribute("err","该账号已被禁用");
                return "back/login";
            }
            Emp emp = empService.findEmpByEmpId(a.getEmpId());
            List<Modules> modulesList = modulesService.findModulesByPosId(emp.getPositionId());
            session.setAttribute("empInfo",emp);
            model.addAttribute("modulesList",modulesList);

        }
        return "back/index";

    }


    @RequestMapping("/logOut")
    public String logOut(HttpSession session){
        session.removeAttribute("empInfo");
        return "redirect:/back/login";
    }


}
